// 主题类型
const THEMES = {
    'light': 'light',
    'dark': 'dark'
};

// 获取当前主题
function getCurrentTheme() {
    return localStorage.getItem('theme') || 'light';
}

// 设置主题
function setTheme(theme) {
    if (THEMES[theme]) {
        localStorage.setItem('theme', theme);
        applyTheme(theme);
        return true;
    }
    return false;
}

// 应用主题
function applyTheme(theme) {
    document.body.classList.remove('theme-light', 'theme-dark');
    document.body.classList.add(`theme-${theme}`);
    
    // 更新主题切换按钮状态
    const themeToggle = document.getElementById('themeToggle');
    if (themeToggle) {
        themeToggle.checked = (theme === 'dark');
    }
}

// 切换主题
function toggleTheme() {
    const currentTheme = getCurrentTheme();
    const newTheme = currentTheme === 'light' ? 'dark' : 'light';
    setTheme(newTheme);
}

// 创建主题切换开关
function createThemeToggle(containerId) {
    const container = document.getElementById(containerId);
    if (!container) return;
    
    const currentTheme = getCurrentTheme();
    
    // 创建主题切换容器
    const themeToggleContainer = document.createElement('div');
    themeToggleContainer.className = 'theme-toggle-container';
    
    // 添加标签
    const label = document.createElement('span');
    label.textContent = getLocalizedString('theme') + ': ';
    themeToggleContainer.appendChild(label);
    
    // 创建开关容器
    const switchContainer = document.createElement('label');
    switchContainer.className = 'theme-switch';
    
    // 创建复选框
    const checkbox = document.createElement('input');
    checkbox.type = 'checkbox';
    checkbox.id = 'themeToggle';
    checkbox.checked = (currentTheme === 'dark');
    
    // 添加事件监听器
    checkbox.addEventListener('change', function() {
        toggleTheme();
    });
    
    // 创建开关滑块
    const slider = document.createElement('span');
    slider.className = 'slider round';
    
    // 组装开关
    switchContainer.appendChild(checkbox);
    switchContainer.appendChild(slider);
    
    themeToggleContainer.appendChild(switchContainer);
    container.appendChild(themeToggleContainer);
}

// 初始化主题
document.addEventListener('DOMContentLoaded', function() {
    // 如果没有设置主题，默认使用亮色主题
    if (!localStorage.getItem('theme')) {
        localStorage.setItem('theme', 'light');
    }
    
    // 应用当前主题
    applyTheme(getCurrentTheme());
});